﻿using System;
using System.Reflection;

namespace Cookbook.Services.Core.Handlers
{
    [AttributeUsage(AttributeTargets.Class)]
    public class MapsToAggregateRootMethodAttribute : AppliesToAggregateRootAttribute
    {
        private readonly string methodName;

        public MapsToAggregateRootMethodAttribute(Type aggregateRootType, string methodName) : base(aggregateRootType)
        {
            this.methodName = methodName;
        }

        public MethodInfo GetMethod()
        {
            MethodInfo methodInfo = AggregateRootType.GetMethod(methodName);
            if (methodInfo == null)
            {
                throw new InvalidOperationException(string.Format(
                    "Class {0} does not have a method named {1}.", AggregateRootType.Name, methodName));    
            }

            return methodInfo;
        }
    }
}